package com.meixiang.fragment.collect;


import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.adapter.CollectAdapter;
import com.meixiang.entity.collect.CollectDateEntity;
import com.meixiang.entity.collect.CollectEntity;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseFragment;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.swipetoloadlayout.base.OnLoadMoreListener;
import com.meixiang.view.swipetoloadlayout.base.OnRefreshListener;
import com.meixiang.view.swipetoloadlayout.base.SwipeToLoadLayout;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 商品
 */
public class CommodityFragment extends BaseFragment implements OnRefreshListener,OnLoadMoreListener{


    @Bind(R.id.swipe_target)
    RecyclerView mSwipeTarget;
    @Bind(R.id.refresh)
    SwipeToLoadLayout mRefresh;
    CollectAdapter mAdapter;
    private String favType = "1" ;
    private CollectEntity mCollectEntity ;
    private List<CollectDateEntity> mCollectList ;
    private List<CollectDateEntity> commodityList ;

    public CommodityFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_commodity, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {
        /*mRefresh.setOnRefreshListener(this);
        mRefresh.setOnLoadMoreListener(this);
        mSwipeTarget.setLayoutManager(new LinearLayoutManager(activity,LinearLayoutManager.VERTICAL,false));
        mAdapter=new CollectAdapter(getContext(),activity);
        mSwipeTarget.setAdapter(mAdapter);*/
    }

    @Override
    protected void setData() {
        //getData(1,true);
    }
    private void getData(int pageNo, final boolean isDownPull){
        HttpParams params = new HttpParams();
        params.put("favType",favType);
        params.put("pageSize","10");
        params.put("pageNo",pageNo+"");
        HttpUtils.post(Config.COLLECT_LIST, params, new HttpCallBack(getActivity()) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if (isDownPull){
                    mAdapter.clearData();
                }
                mCollectEntity = new Gson().fromJson(result.toString(),CollectEntity.class);
                mCollectList = mCollectEntity.getmCollectDateList();
                commodityList = new ArrayList<>() ;
                for(int i=0;i<mCollectList.size();i++){
                    if(favType.equals(mCollectList.get(i).getFavType())){ //去掉不是商品的收藏数据
                        commodityList.add(mCollectList.get(i)) ;
                    }
                }
                if (commodityList.size()==0){
                    showNoData("暂无收藏");
                }else{
                    mAdapter.addData(commodityList);
                }
            }

            @Override
            public void onError(String code, String message) {

            }

            @Override
            public void onAfter(boolean isFromCache, JSONObject jsonObject, Call call, Response response, Exception e) {
                super.onAfter(isFromCache, jsonObject, call, response, e);
                onComplete(mRefresh);
            }
        });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @Override
    public void onLoadMore() {
        if (mCollectEntity.getTotalPage()>mCollectEntity.getPageNo()){
            getData(mCollectEntity.getPageNo()+1,false);
        }else{
            AbToastUtil.showToast(activity,"已无更多收藏");
            onComplete(mRefresh);
        }
    }

    @Override
    public void onRefresh() {
        getData(1,true);
    }
}
